package com.alibaba.citrus.cr.forecast.facade.converter;

import com.alibaba.citrus.cr.forecast.common.DateUtils;
import com.alibaba.cz.base.tool.BeanConverter;
import com.epoch.app.crforecastsales.dto.request.QueryForecastRequest;
import com.epoch.app.crforecastsales.model.dto.PageQueryForecastRequest;
import com.epoch.app.crforecastsales.model.dto.SearchForecastOrderRequest;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

/**
 * @Author yuanDeng
 * @Date 2021/9/16 10:00
 **/
public class QueryForecastRequestConverter {
    public static ForecastOrderSDO toQueryForecastResponse(QueryForecastRequest source) {
        if (source == null) {
            return null;
        }
        ForecastOrderSDO target = new ForecastOrderSDO();
        target.setCustomerSDO(source.getCustomerSDO());
        target.setForcecastTypeSDO(source.getForcecastTypeSDO());
        target.setGoodsSDO(source.getGoodsSDO());
        target.setForecastOrderLineSDOList(source.getForecastOrderLineSDOList());
        target.setId(source.getId());
        target.setIds(source.getIds());
        target.setBizCode(source.getBizCode());
        target.setBusinessCode(source.getBusinessCode());
        target.setOuterOrderId(source.getOuterOrderId());
        target.setOrderTag(source.getOrderTag());
        target.setCustomerCode(source.getCustomerCode());
        target.setCustomerName(source.getCustomerName());
        target.setOrganizationCode(source.getOrganizationCode());
        target.setOrganizationName(source.getOrganizationName());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setDepartmentName(source.getDepartmentName());
        target.setProductGroupCode(source.getProductGroupCode());
        target.setProductGroupName(source.getProductGroupName());
        target.setSaleChannelCode(source.getSaleChannelCode());
        target.setSaleChannelName(source.getSaleChannelName());
        target.setChannelCode(source.getChannelCode());
        target.setChannelName(source.getChannelName());
        target.setShipAddressCode(source.getShipAddressCode());
        target.setShipAddressInfo(source.getShipAddressInfo());
        target.setReceiveAddressCode(source.getReceiveAddressCode());
        target.setReceiveDetailAddress(source.getReceiveDetailAddress());
        target.setContact(source.getContact());
        target.setContactPhone(source.getContactPhone());
        target.setTransportModeCode(source.getTransportModeCode());
        target.setTransportModeName(source.getTransportModeName());
        target.setCreateReason(source.getCreateReason());
        target.setRemark(source.getRemark());
        target.setWareHouseCode(source.getWareHouseCode());
        target.setWareHouseName(source.getWareHouseName());
        target.setOrderTotalFee(source.getOrderTotalFee());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setTotalReplyAmount(source.getTotalReplyAmount());
        target.setTotalPredictAmount(source.getTotalPredictAmount());
        target.setCreditBalance(source.getCreditBalance());
        target.setCustomerReferenceNo(source.getCustomerReferenceNo());
        target.setCustomerExpectTDate(source.getCustomerExpectTDate());
        target.setReplyTime(source.getReplyTime());
        target.setReplyStatus(source.getReplyStatus());
        target.setRefuseStatus(source.getRefuseStatus());
        target.setRefuseTime(source.getRefuseTime());
        target.setStatus(source.getStatus());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setDealStatus(source.getDealStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setIdemId(source.getIdemId());
        target.setVaildBeginTime(source.getVaildBeginTime());
        target.setValidEndTime(source.getValidEndTime());
        target.setGmtCreate(source.getGmtCreate());
        target.setGmtModified(source.getGmtModified());
        target.setCreatorId(source.getCreatorId());
        target.setCreatorName(source.getCreatorName());
        target.setFeatures(source.getFeatures());
        target.setStart(source.getStart());
        target.setLimit(source.getLimit());
        target.setChannelCodes(target.getChannelCodes());
        target.setSaleChannelCodes(target.getSaleChannelCodes());
        target.setProductGroupCodes(target.getProductGroupCodes());
        target.setDocReasons(target.getDocReasons());
        target.setDepartmentCodes(target.getDepartmentCodes());
        return target;
    }

    public static void main(String[] args) {
        BeanConverter.generateCode(QueryForecastRequest.class, ForecastOrderSDO.class);
    }

    public static SearchForecastOrderRequest toSearchForecastOrderRequest(PageQueryForecastRequest request) {
        SearchForecastOrderRequest searchForecastOrderRequest = new SearchForecastOrderRequest();


        if (CollectionUtils.isEmpty(request.getCustomerExpectPeriodDate())) {
            //期望交货日期
            searchForecastOrderRequest.setCustomerExpectStartDate(request.getCustomerExpectTDate() == null ? null : DateUtils.getFirstTime(request.getCustomerExpectTDate()));
            searchForecastOrderRequest.setCustomerExpectEndDate(request.getCustomerExpectTDate() == null ? null : DateUtils.getLastTime(request.getCustomerExpectTDate()));
        } else {
            //修改逻辑
            Date startDate = StringUtils.isBlank(request.getCustomerExpectPeriodDate().get(0))  ? null : DateUtils.string2Date(request.getCustomerExpectPeriodDate().get(0));
            Date endDate = StringUtils.isBlank(request.getCustomerExpectPeriodDate().get(1))  ? null : DateUtils.string2Date(request.getCustomerExpectPeriodDate().get(1));

            searchForecastOrderRequest.setCustomerExpectStartDate(startDate);
            searchForecastOrderRequest.setCustomerExpectEndDate(endDate);


        }

        searchForecastOrderRequest.setUpdateStartTime(request.getUpdateTime() == null ? null : DateUtils.string2Date(request.getUpdateTime().get(0)));
        searchForecastOrderRequest.setUpdateEndTime(request.getUpdateTime() == null ? null : DateUtils.string2Date(request.getUpdateTime().get(1)));
//        searchForecastOrderRequest.setUpdateStartTime(request.getUpdateTime() == null ? null : com.alibaba.citrus.cr.common.util.DateUtils.convertToMillisecondStr(request.getUpdateTime().get(0)));
//        searchForecastOrderRequest.setUpdateEndTime(request.getUpdateTime() == null ? null : com.alibaba.citrus.cr.common.util.DateUtils .convertToMillisecondStr(request.getUpdateTime().get(1)));
        // 查询生效日期当天及之前的待回复&部分回复的单据

        //生效日期
        if (CollectionUtils.isEmpty(request.getValidPeriodDate())) {
            searchForecastOrderRequest.setValidStartDate(request.getValidDate() == null ? null : DateUtils.getFirstTime(request.getValidDate()));
            searchForecastOrderRequest.setValidEndDate(request.getValidDate() == null ? null : DateUtils.getLastTime(request.getValidDate()));
        } else {

            Date startDate = StringUtils.isBlank(request.getValidPeriodDate().get(0))  ? null : DateUtils.string2Date(request.getValidPeriodDate().get(0));
            Date endDate = StringUtils.isBlank(request.getValidPeriodDate().get(1))  ? null : DateUtils.string2Date(request.getValidPeriodDate().get(1));

            searchForecastOrderRequest.setValidStartDate(startDate);
            searchForecastOrderRequest.setValidEndDate(endDate);


        }


        searchForecastOrderRequest.setGmtStartCreate(request.getGmtCreate() == null ? null : DateUtils.string2Date(request.getGmtCreate().get(0)));
        searchForecastOrderRequest.setGmtEndCreate(request.getGmtCreate() == null ? null : DateUtils.string2Date(request.getGmtCreate().get(1)));
        searchForecastOrderRequest.setId(request.getId());
        //黔雷 和前端调整字段
        //searchForecastOrderRequest.setMainOrderBizId(request.getId());
        searchForecastOrderRequest.setOrganizationCodeList(request.getOrganizationCodeList());
        searchForecastOrderRequest.setCustomerCode(request.getCustomerCode());
        searchForecastOrderRequest.setCustomerCodeList(request.getCustomerCodeList());
        searchForecastOrderRequest.setProductCode(request.getProductCode());
        searchForecastOrderRequest.setOrderTypeList(request.getOrderTypeList());
        if (request.getIsCollect() != null && request.getIsCollect()) {
            //批量汇总页面查询标准预报订单-01+补货预报订单-03
            searchForecastOrderRequest.setOrderTypeList(Arrays.asList("01", "03"));
            searchForecastOrderRequest.setSubCollectStatusList(Arrays.asList(0));
        } else {
            //批量汇总页面排除已汇总的预报单，其他页面查询包含已汇总单据
            searchForecastOrderRequest.setSubCollectStatusList(Arrays.asList(0, 1));
        }
        searchForecastOrderRequest.setChannelCodeList(request.getChannelCodes());
        searchForecastOrderRequest.setReceiveAddressCode(request.getReceiveAddressCode());
        searchForecastOrderRequest.setReceiveAddressCodeList(request.getReceiveAddressCodeList());
        searchForecastOrderRequest.setDepartmentCodeList(request.getDepartmentCodes());
        searchForecastOrderRequest.setCreatorId(request.getCreatorId());
        searchForecastOrderRequest.setStatusCode(request.getStatusCode());
        searchForecastOrderRequest.setCustomerReferenceNo(request.getCustomerReferenceNo());
        searchForecastOrderRequest.setModifierId(request.getModifierId());
        searchForecastOrderRequest.setDetailStatusList(request.getDetailStatusList() == null ? null : request.getDetailStatusList());
        if (CollectionUtils.isNotEmpty(request.getDealStatusList())) {
            searchForecastOrderRequest.setDealStatusList(request.getDealStatusList());
        }

        searchForecastOrderRequest.setRefuseReasonList(request.getRefuseReasonList());
        searchForecastOrderRequest.setProductGroupCodeList(request.getProductGroupCodes());
        searchForecastOrderRequest.setSaleChannelCodeList(request.getSaleChannelCodes());
        searchForecastOrderRequest.setDocReasonList(request.getDocReasons());
        searchForecastOrderRequest.setRemark(request.getRemark());
        searchForecastOrderRequest.setWareHouseCode(request.getWareHouseCode());
        searchForecastOrderRequest.setWareHouseCodes(request.getWareHouseCodes());
        searchForecastOrderRequest.setTransportModeCodeList(request.getTransportModeCode() == null ? null : Collections.singletonList(request.getTransportModeCode()));
        searchForecastOrderRequest.setContact(request.getContact());
        searchForecastOrderRequest.setContactPhone(request.getContactPhone());
        searchForecastOrderRequest.setCreatorName(request.getCreatorName());
        searchForecastOrderRequest.setMainOrderBizId(request.getOrderBizId());
        //searchForecastOrderRequest.setId(request.getId());
        searchForecastOrderRequest.setModifierName(request.getModifierName());
        searchForecastOrderRequest.setIsPage(request.getIsPage() == null);
        searchForecastOrderRequest.setStart(request.getStart());
        searchForecastOrderRequest.setLimit(request.getLimit());
        searchForecastOrderRequest.setSubRemark(request.getSubRemark());
        searchForecastOrderRequest.setMainStatusList(request.getMainStatusList());
        searchForecastOrderRequest.setAnalysisReasonCodeList(request.getAnalysisReasonCodeList());
        searchForecastOrderRequest.setSubLineBizId(request.getSubLineBizId() == null ? null : Collections.singletonList(request.getSubLineBizId()));
        searchForecastOrderRequest.setSalesmanCode(request.getSalesmanCode());
        searchForecastOrderRequest.setOrderForm(request.getOrderForm());
        searchForecastOrderRequest.setOrderField(request.getOrderField());
        //是否是小程序提报
        if (null!=request.getAppCreate()){
            searchForecastOrderRequest.setAppCreate(request.getAppCreate());
        }
        //是否存在活动冲突
        if (null!=request.getIsActivityConflict()){
            searchForecastOrderRequest.setIsActivityConflict(request.getIsActivityConflict());
        }
        searchForecastOrderRequest.setGoodsCodeLike(request.getGoodsCodeLike());
        //添加是否展示非件单位逻辑
        if (StringUtils.isNotBlank(request.getNonPartsUnit())) {
            searchForecastOrderRequest.setNonPartsUnit("PC");
        }
        //创建人ID集合
        searchForecastOrderRequest.setCreatorIdList(request.getCreatorIdList());

        //小程序提报筛选
        searchForecastOrderRequest.setAppCreate(request.getAppCreate());
        searchForecastOrderRequest.setOutIds(request.getOutIds());
        searchForecastOrderRequest.setGoodsIds(request.getGoodsIds());
//        searchForecastOrderRequest.setDir();
//        searchForecastOrderRequest.setAsc();
//        searchForecastOrderRequest.setFields();
//        searchForecastOrderRequest.setSize();
//        searchForecastOrderRequest.setPage();
        return searchForecastOrderRequest;

    }


}
